<template>
  <div class="cy-form">
    <div class="header">
      <slot name="header"></slot>
    </div>
    <el-form :label-width="labelWidth">
      <el-row>
        <template v-for="item in formItems" :key="item.label">
          <el-col v-bind="colLayout">
            <el-form-item
              v-if="!item.isHidden"
              :label="item.label"
              :rules="item.rules"
              :style="itemStyle"
            >
              <template
                v-if="item.type === 'input' || item.type === 'password'"
              >
                <el-input
                  :placeholder="item.placeHolder"
                  :show-password="item.type === 'password'"
                  v-bind="item.otherOptions"
                  v-model="formData[`${item.field}`]"
                />
              </template>
              <template v-else-if="item.type === 'select'">
                <el-select
                  :placeholder="item.placeHolder"
                  v-bind="item.otherOptions"
                  style="width: 100%"
                  v-model="formData[`${item.field}`]"
                >
                  <!-- 1.a
                    :model-value=modelValue[`${item.field}`]
                    @update:modelValue="handleValueChange($event, item.field)"
                   -->
                  <el-option
                    v-for="option in item.options"
                    :key="option.value"
                    :value="option.value"
                    >{{ option.title }}</el-option
                  >
                </el-select>
              </template>
              <template v-else-if="item.type === 'datepicker'">
                <el-date-picker
                  v-bind="item.otherOptions"
                  style="width: 100%"
                  v-model="formData[`${item.field}`]"
                >
                </el-date-picker>
              </template>
            </el-form-item>
          </el-col>
        </template>
      </el-row>
    </el-form>
    <div class="footer">
      <slot name="footer"></slot>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, PropType, ref, watch } from 'vue'
import { IFormItem } from '../types/index'

export default defineComponent({
  props: {
    //双向绑定，父传子
    modelValue: {
      type: Object,
      required: true
    },
    formItems: {
      type: Array as PropType<IFormItem[]>,
      default: () => []
    },
    labelWidth: {
      type: String,
      default: '100px'
    },
    itemStyle: {
      type: Object,
      default: () => ({
        padding: '10px 30px'
      })
    },
    colLayout: {
      type: Object,
      default: () => ({
        xl: 6, //>1920 4分
        lg: 8,
        md: 12,
        sm: 24,
        xs: 24
      })
    }
  },
  //实时拿到内部最新的值
  setup(props, { emit }) {
    const formData = ref({ ...props.modelValue })
    //子传父，当数据改变时，则触发我们的事件，派发事件并传递出去最新的值
    watch(
      formData,
      (newValue) => {
        emit('update:modelValue', newValue)
      },
      {
        deep: true,
        immediate: true
      }
    )
    //1.b 设置点击change事件，子传父通信，改变内部的事件，更新modelValue的值
    // const handleValueChange = (value: any, field: string) => {
    //emit('update:modelValue', {...props.modelValue, [field]: value})
    //}
    return {
      formData
    }
  }
})
</script>

<style scoped>
.header {
  color: red;
}
.footer {
  text-align: right;
  padding: 0 50px 20px 0;
}
.cy-form {
  padding-top: 20px;
}
</style>
